package org.jetlinks.pro.koudai.web;


import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.hswebframework.ezorm.rdb.mapping.defaults.SaveResult;
import org.hswebframework.web.authorization.annotation.QueryAction;
import org.hswebframework.web.authorization.annotation.Resource;
import org.hswebframework.web.authorization.annotation.SaveAction;
import org.jetlinks.pro.koudai.Services.TakedExperimentService;
import org.jetlinks.pro.koudai.Services.TakedQuestionService;
import org.jetlinks.pro.koudai.entity.TakedExperimentAddParam;
import org.jetlinks.pro.koudai.entity.TakedExperimentEntity;
import org.jetlinks.pro.koudai.entity.TakedQuestionEntity;
import org.jetlinks.pro.koudai.tenant.KoudaiAssetType;
import org.jetlinks.pro.tenant.AssetType;
import org.jetlinks.pro.tenant.crud.TenantCorrelatesAccessCrudController;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.util.function.Tuple2;

import javax.annotation.Nonnull;
import java.util.List;
import java.util.function.Function;

@RestController
@RequestMapping("/taked-experiment")
@Resource(id = "taked-experiment-manager", name = "实验项目管理")
@Tag(name = "实验项目管理")
@AllArgsConstructor
public class TakedExperimentController implements TenantCorrelatesAccessCrudController<TakedExperimentEntity, String> {

    private final TakedExperimentService takedExperimentService;

    private final TakedQuestionService takedQuestionService;


    //获取详情
    @PostMapping("/add")
    @SaveAction
    @Operation(summary = "布置实验")
    public Mono<Long> addExperiment(@RequestBody TakedExperimentAddParam param) {



        return Flux.fromIterable(param.getUserList())
            .flatMap(user -> {
                TakedExperimentEntity  takedExperiment = new TakedExperimentEntity();

                takedExperiment.setExperimentStudentId(param.getTakedExperiment().getExperimentStudentId());
                takedExperiment.setQuestionId(param.getTakedExperiment().getQuestionId());
                takedExperiment.setCreateTeacherBy(param.getTakedExperiment().getCreateTeacherBy());
                takedExperiment.setVideoTutorialType(param.getTakedExperiment().getVideoTutorialType());
                takedExperiment.setCreateSchoolBy(param.getTakedExperiment().getCreateSchoolBy());
                takedExperiment.setExperimentClassId(param.getTakedExperiment().getExperimentClassId());
                takedExperiment.setExperimentId(param.getTakedExperiment().getExperimentId());

                takedExperiment.setExperimentStudentId(user);
                return takedExperimentService
                    .save(takedExperiment)
                    .then(Mono.just(takedExperiment))
                    .flatMap(payload2 -> Flux.fromIterable(payload2.getQuestionId())
                        .map(qId -> new TakedQuestionEntity(payload2.getId(), qId))
                        .as(takedQuestionService::save));
            })
            .count();


    }

    @Override
    public TakedExperimentService getService() {
        return takedExperimentService;
    }

    @Nonnull
    @Override
    public AssetType getAssetType() {
        return KoudaiAssetType.takedExperiment;
    }

    @Nonnull
    @Override
    public Function<TakedExperimentEntity, ?> getAssetIdMapper() {
        return TakedExperimentEntity::getId;
    }

    @Nonnull
    @Override
    public String getAssetProperty() {
        return "takedExperimentService";
    }
}
